package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ScreenUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.App;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.User;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.common.Constant;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.InputStream;
import java.util.Scanner;

import butterknife.BindView;

public class SplashActivity extends BaseActivityImpl implements BaseActivityImpl.UnnecessaryLogin, BaseActivityImpl.UnnecessaryRequestGlobalParams {
    @BindView(R.id.view_shadow)
    View viewShadow;
    SharedPreferences sharedPreferences;

    @Override
    public AppCompatActivity getSelfActivity() {
        return this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView.setWebContentsDebuggingEnabled(false);

        sharedPreferences = getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        String token = sharedPreferences.getString(Constant.SpTag.TOKEN, "");

        viewShadow.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams layoutParams = viewShadow.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ScreenUtils.getScreenHeight() / 3 * 2;
                viewShadow.setLayoutParams(layoutParams);
                viewShadow.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        SharedPreferences sharedPreferences = getSharedPreferences(Constant.SP_NAME_AREA_SORT_BY_FIRST_LETTER, Context.MODE_PRIVATE);
        if (!sharedPreferences.contains(Constant.SpTag.AREA_SORT_BY_FIRST_LETTER)) {
            try {
                InputStream input = getAssets().open("area.txt");
                String json = null;
                //格式转换
                Scanner scanner = new Scanner(input, "UTF-8").useDelimiter("\\A");
                if (scanner.hasNext()) {
                    json = scanner.next();
                }
                input.close();

                if (!TextUtils.isEmpty(json)) {
                    JsonElement jsonElement = new JsonParser().parse(json);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    String uuid = jsonElement.getAsJsonObject().get("uuid").getAsString();
                    editor.putString(Constant.SpTag.AREA_SORT_BY_FIRST_LETTER_UUID, uuid);
                    editor.putString(Constant.SpTag.AREA_SORT_BY_FIRST_LETTER, json);
                    editor.apply();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (!TextUtils.isEmpty(token)) {
            RequestManager.setToken(token);
            flushLogin();
        } else {
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void flushLogin() {
        requestManager.request(requestManager.mJavaRetrofitService.flushLogin(), new BaseObserverImpl<User>() {
            @Override
            public void onSuccess(User user) {
                super.onSuccess(user);
                App.setUser(user);
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onOtherCode(int code) {
                super.onOtherCode(code);
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Toast.makeText(mContext, "无法连接到服务器，请重新打开app", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}

































